<?php
/**
 *  
 *
  */
namespace Org\Util;
class Rhebk3 {

    
     public function hebk3_balance() {
		 $day=date("Y-m-d");
		 $day_time=strtotime($day);
		 $insert_data=array();
		 $lm=array();
 				  $n_time=time(); 
 				    $condition['c_id']=19;	
					//$condition['add_time'] = array(array('gt',$day_time)) ; 
 					$max_c_no=M('lottery_results')->where($condition)->max('c_no'); //获取当天最大期数
					 
					$url='http://a.apiplus.net/newly.do?token=5ee5aca63461f954&code=hebk3&rows=1&format=json'; //接口地址
					
					$new_result=get_http($url);
					$result=json_decode($new_result,true);
 					if($max_c_no!=$result['data'][0]['expect']){
				 	// print_r($result['data'][0]['expect']);exit;
					S('hebk3_'.$result['data'][0]['expect'],$result['data'][0]['opencode']);  //存入缓存  方便计算两面长龙算法
					$insert_data['c_no']=$result['data'][0]['expect'];
					$insert_data['c_id']=19;
					$insert_data['c_name']='河北快3';
					$insert_data['add_time']=$result['data'][0]['opentimestamp'];
					$insert_data['lottery_result']=$result['data'][0]['opencode'];
					$insert_data['status']=0;
					$num= explode(",",$result['data'][0]['opencode']);
					$sum=array_sum($num);
					$lm[0]=$sum;
 		            $lm[1]=$this->zh_dx($sum);
 				    $n_lm=implode(",",$lm);
					$insert_data['lm']=$n_lm;
					if($max_c_no+1==$result['data'][0]['expect']){  //计算两面长龙结果
  					   
					   $flag=1;
					   $flag_dx=0;
					    
					   $k=1;
					   do{
						   
						   $old_lottery=$result['data'][0]['expect']-$k;
						   $gxk3_result=S('gxk3_'.$old_lottery);
			 
							if(empty($gxk3_result))
							{
							$condition1['c_id']=7;
							$condition1['c_no']=$old_lottery;
							$gxk3_result1=M('lottery_results')->where($condition1)->getField('lottery_result');
							if($gxk3_result1){
							S('gxk3_'.$old_lottery,$gxk3_result1);
							}else{
								$flag=0;
								}
							}	
 						   $num1[$k-1]= explode(",",$gxk3_result);
						   $sum1[$k-1]=array_sum($num1[$k-1]);
						  
						   
						   
							   //计算总和大小两面长龙
						   $dx1=$this->zh_dx($sum1[$k-1]);
						   if($flag_dx==0){
							   
							  if($dx1==$lm[1]){
							   $b=$k+1;
							   //DX=X,1
								 if($dx1=='小'){
								 $zh_dx='DX=X,'.$b;
								 }else{
								 $zh_dx='DX=D,'.$b;
									 }
								 if($num1[$k-1][0]==$num1[$k-1][1]&&$num1[$k-1][0]==$num1[$k-1][2]){
								 $zh_dx='DX=N,'.$b;	 
									 }	 
							   }else{
							   $flag_dx=1;	 
								   }
						   }
						   
						    
						   
						   $k++;
					 
						  if($flag_dx==1){
							// if($k<11){
							   $flag=0;
							   }
							// $flag=0;  
						   }while($flag==1);
					  
					  
					   
					   if($zh_dx){
					   $s[]=$zh_dx;	
					   }
					    
					   
					   $n_s=implode(";",$s);	
						//print_r($n_s);exit;
					  $insert_data['detail']=$n_s;	
						}
						
					$insert_result=M('lottery_results')->data($insert_data)->add();	
				 	if(!$insert_result){
						 
						 file_put_contents("test.txt", date("Y-m-d H:i:s") . "获取河北快3结果存入失败！" . "\r\n<br>", FILE_APPEND);  	
							}else{
						$hebk3_result = new \Org\Util\Jshebk3();  //河北快3
        $hebk3_result->hebk3_balance();
					}
 					}
  					 
		return; 		
     }
	
	 public function zh_dx($sum){  //总和大小
      if($sum>=11){
		   $dx='大';	 
			 }else{
			 $dx='小';	 
				 }
		return $dx;
     }
	 
}